package com.ruoyi.myutils;



import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class UtilsWuda {

    @Value("${file.shpFile}")
    private static String shpFile;

    /**
     *
     * @param input
     * @return boolean
     */
    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input))
            return true;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }


    public static File convertMultipartFileToFile(MultipartFile multipartFile) {
        File file = new File(multipartFile.getOriginalFilename());
        try (FileOutputStream fos = new FileOutputStream(file);
             InputStream is = multipartFile.getInputStream()) {
            int len;
            byte[] buffer = new byte[1024];
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static void deleteFolder(@NotNull File folder) {
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    deleteFolder(file);
                } else {
                    file.delete();
                }
            }
        }
        folder.delete();
    }

    /**
     * 删除KML转换过程中生成的临时文件
     * @param kmlFile 原始KML文件
     */
    public static void deleteTempFiles(File kmlFile) {
        if (kmlFile == null || !kmlFile.exists()) {
            return;
        }

        try {
            // 删除原始KML文件
            if (kmlFile.delete()) {
                System.out.println("已删除临时KML文件: " + kmlFile.getAbsolutePath());
            } else {
                System.err.println("无法删除临时KML文件: " + kmlFile.getAbsolutePath());
            }

            // 获取文件名(不含扩展名)
            String fileName = FilenameUtils.removeExtension(kmlFile.getName());
            File parentDir = kmlFile.getParentFile();

            // 删除SHP相关的所有文件(.shp, .shx, .dbf, .prj等)
            if (parentDir != null && parentDir.exists() && parentDir.isDirectory()) {
                File[] shpFiles = parentDir.listFiles((dir, name) ->
                        name.startsWith(fileName) && !name.equals(kmlFile.getName()));

                if (shpFiles != null) {
                    for (File shpFile : shpFiles) {
                        if (shpFile.delete()) {
                            System.out.println("已删除临时文件: " + shpFile.getAbsolutePath());
                        } else {
                            System.err.println("无法删除临时文件: " + shpFile.getAbsolutePath());
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.err.println("删除临时文件时出错: " + e.getMessage());
            e.printStackTrace();
        }
    }}
